package com.tcdp.daos;

import java.util.List;

import javax.persistence.Tuple;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.ParameterExpression;
import javax.persistence.criteria.Root;

import com.tcdp.models.WebInfo;

public class WebInfoDao extends AbstractDao {

	public WebInfo findById(int id){
		WebInfo webInfo = em.find(WebInfo.class, id);
		return webInfo;
	}
	
	public String findByName(String name){
		String webInfo = null;		
		CriteriaBuilder cb = em.getCriteriaBuilder();
		CriteriaQuery<Tuple> cq = cb.createQuery(Tuple.class);
		Root<WebInfo> root = cq.from(WebInfo.class);
		ParameterExpression<String> nameParameter = cb.parameter(String.class, "webInfoName");
		cq.multiselect(root.get("webInfoValue")).where(cb.equal(root.<String>get("webInfoName"), nameParameter));
		TypedQuery<Tuple> tq = em.createQuery(cq);
		tq.setParameter("webInfoName", name);
		List<Tuple> tupleResult = tq.getResultList();
		for(Tuple tuple: tupleResult){
			webInfo = tuple.get(0).toString();
		}
		
		return webInfo;
	}
	
}
